﻿/*最后修改 2013/9/5 13:13 by cyh
 * by cyh
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Search.Highlight;
using Lucene.Net.Store;
using Lucene.Net.Index;
using System.Drawing;
using PanGu;
using PanGu.HighLight;
using Lucene.Net.Analysis.PanGu;
using System.Collections;
using Version = Lucene.Net.Util.Version;
using System.Collections.Generic;

public partial class wxapi : System.Web.UI.Page
{
    static public List<string> SplitKeyWords(string keywords, Analyzer analyzer)
    {
        System.IO.StreamReader reader = new System.IO.StreamReader(PanGu.Framework.Stream.WriteStringToStream(keywords,
            Encoding.UTF8), Encoding.UTF8);

        TokenStream tokenStream = analyzer.TokenStream("", reader);

        global::Lucene.Net.Analysis.Token token = tokenStream.Next();

        List<string> result = new List<string>();

        while (token != null)
        {
            result.Add(keywords.Substring(token.StartOffset(), token.EndOffset() - token.StartOffset()));
            token = tokenStream.Next();
        }

        return result;

    }

    static public string GetKeyWordsSplitBySpace(string keywords, PanGuTokenizer ktTokenizer)
    {
        StringBuilder result = new StringBuilder();

        ICollection<WordInfo> words = ktTokenizer.SegmentToWordInfos(keywords);

        foreach (WordInfo word in words)
        {
            if (word == null)
            {
                continue;
            }

            result.AppendFormat("{0}^{1}.0 ", word.Word, (int)Math.Pow(3, word.Rank));
        }

        return result.ToString().Trim();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string echos = Request["echoStr"];
        if (echos == null)//判断echostr参数是否为Null，若是则进行非验证操作，若不是，则进行与微信服务器的验证操作
        {
            string postStr = "";
            if (Request.HttpMethod.ToLower() == "post")
            {
                Stream s = System.Web.HttpContext.Current.Request.InputStream;
                byte[] b = new byte[s.Length];
                s.Read(b, 0, (int)s.Length);
                postStr = Encoding.UTF8.GetString(b);
                if (!string.IsNullOrEmpty(postStr))
                {
                    ResponseMsg(postStr);
                }
            }
        }
        else
        {
            Valid(echos);
        }
    }
    #region 验证签名
   
    private void Valid(string echoStr)
    {
            if (CheckSignature() == true)
            {
                Response.Write(echoStr);
                Response.End();
            }
        
    }
    /// <summary>
    /// 验证微信签名
    /// </summary>
    /// <returns></returns>
    private bool CheckSignature()
    {
        string signature = Request["signature"].ToString();
        string timestamp = Request["timestamp"].ToString();
        string nonce = Request["nonce"].ToString();
        string[] ArrTmp = { "wbktypt", timestamp, nonce };
        Array.Sort(ArrTmp);
        //字典排序
        string tmpStr = string.Join("", ArrTmp);
        tmpStr = FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1");
        //sha1加密
        tmpStr = tmpStr.ToLower();

        if (tmpStr == signature)
        {
            return true;
        }
        else return false;
    }
    #endregion

    private List<string> search(string content,string id)
    {
        int i = 0;
        int total = 0;
        int pp = 0;
        List<string> result = new List<string>(10);
        try
        {

            TimeSpan duration;
            DateTime start = DateTime.Now;
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();
            SqlCommand get_realid = new SqlCommand("select ENTER_ID from INFO_ENTER where ENTER_WXACOUNT=@ac", sc1);
            get_realid.Parameters.AddWithValue("@ac", id);
            string enterid = get_realid.ExecuteScalar().ToString();
            sc1.Close();

            string indexDirectory = Server.MapPath("index/index_"+enterid);
            IndexSearcher searcher = new IndexSearcher(indexDirectory);
            // parse the query, "text" is the default field to search
            string[] f={"text","name"};
            MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_29, f, new PanGuAnalyzer());

            List<string> q = SplitKeyWords(content, new PanGuAnalyzer());
            StringBuilder query_strb = new StringBuilder("");
            foreach (string duck in q)
            {
                query_strb.Append(duck + " ");
            }
            string str_query = query_strb.ToString();
            Query query = parser.Parse(str_query);

            TopDocs hits = searcher.Search(query, 15);
            total = hits.totalHits;
            if (total == 0)
                result.Add("没有搜索结果!");
            else
            {
                //result = str_query;
                PanGu.HighLight.Formatter formatter = new PanGu.HighLight.SimpleHTMLFormatter("", "");
                PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(formatter, new Segment());
                highlighter.FragmentSize = 50;
                if (total > 5)
                {
                    pp = total;
                    total = 5;
                }
                for (i = 0; i < total; i++)
                {
                    Document doc = searcher.Doc(hits.scoreDocs[i].doc);
                    TokenStream stream = new PanGuAnalyzer().TokenStream("", new StringReader(doc.Get("text")));
                    String sample_temp = highlighter.GetBestFragment(str_query, doc.Get("text"));
                    result.Add(doc.Get("pid")+doc.Get("name") + "\n" + "--------------------" + "\n" + sample_temp + "\n" + "--------------------" + "\n");
                }
                searcher.Close();
                duration = DateTime.Now - start;
                //result += "total hits: "+pp.ToString()+"Time spends: " + duration.TotalMilliseconds.ToString();
            }
            return result;
        }
        catch (Exception e)
        {
            result.Add("您输入的信息格式无法识别!");
            result.Add(AppDomain.CurrentDomain.BaseDirectory + "\n" + total.ToString() + " " + i.ToString() + e.GetType().ToString() + e.Message + e.StackTrace);
            return result;
            //return  AppDomain.CurrentDomain.BaseDirectory+"\n"+total.ToString()+" "+ i.ToString() + e.GetType().ToString() + e.Message + e.StackTrace;
        }
    }
    private void ResponseMsg(string weixinXML)
    {
        //回复消息的部分:你的代码写在这里
        String Url1 = Request.ServerVariables["Referer"];
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(weixinXML);
        XmlNodeList list = doc.GetElementsByTagName("xml");
        XmlNode xn = list[0];
        string FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
        string ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;
        string msgtype = xn.SelectSingleNode("//MsgType").InnerText;
        
        //先判断用户交互的平台微信号，如果是主平台，检验用户是否是第一次关注平台，若是，则返回第一次欢迎信息，若不是，则按文本信息进行下一步判断，先检验用户是否已注册，第二步进行关键字判断。如果是子平台，仍检验用户openid是否存在于ind_care表中
        
        if (ToUserName.Equals("gh_4166fe56dea0")==true)//主平台微信号
        {
            //主平台操作,先检验是否是关注事件推送
            #region 主平台操作
            if (msgtype.Equals("event") == true)
            {
                string ev = xn.SelectSingleNode("//Event").InnerText;
                string ev_key = xn.SelectSingleNode("//EventKey").InnerText;
                 if (ev.Equals("subscribe") == true)
                {
                    string reg_url = "http://lcsy2008.eicp.net/wbkpro/mobile_register.aspx?openid="+FromUserName;//添加能从互联网直接访问的完整的mobile_register路径
                    string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang3.jpg";
                    string strresponse1 = "<xml>";
                    strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                    strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                    strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                    strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                    strresponse1 = strresponse1 + "<ArticleCount>2</ArticleCount>";
                    strresponse1 = strresponse1 + "<Articles>";
                    strresponse1 = strresponse1 + "<item>";
                    strresponse1 = strresponse1 + " <Title><![CDATA[若不是微百科用户，请点击前往注册!]]></Title>";
                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" +image_url+ "]]></PicUrl>";
                    strresponse1 = strresponse1 + "<Url><![CDATA[" + reg_url + "]]></Url>";
                    strresponse1 = strresponse1 + "</item>";

                    strresponse1 = strresponse1 + "<item>";
                    strresponse1 = strresponse1 + " <Title><![CDATA[若已经是注册用户，则无需登陆直接使用即可!]]></Title>";
                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                    strresponse1 = strresponse1 + "<Url><![CDATA[" + "]]></Url>";
                    strresponse1 = strresponse1 + "</item>";

                    strresponse1 = strresponse1 + "</Articles>";
                    strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                    strresponse1 = strresponse1 + "</xml>";
                  
                    Response.Write(strresponse1);
                }
            }
            //不是关注事件，先检验用户是否注册
            else
            {
            SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc.Open();
            SqlCommand com_check_regist = new SqlCommand();
            com_check_regist.Connection = sc;
            com_check_regist.CommandText = "select IND_ID from INFO_IND where IND_OPENID=@id";
            com_check_regist.Parameters.AddWithValue("@id", FromUserName);
            object a = com_check_regist.ExecuteScalar();
            if (a != null)//用户已注册
            {
                string ind_id = a.ToString();

                if (msgtype.Equals("text") == true)//文本信息处理
                {
                    string content = xn.SelectSingleNode("//Content").InnerText;

                    #region 关键字判断
                   
                    if (content.Equals("CXXF") == true || content.Equals("cxxf") == true)
                    {
                        string a1_url="http://lcsy2008.eicp.net/wbkpro/mobile_xiaofeijl.aspx?ind="+ind_id;//添加能从互联网直接访问的完整的mobile_xiaofeijl路径,query=ind=ind_id
                        string strresponse1 = "<xml>";
                        strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                        strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                        strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                        strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                        strresponse1 = strresponse1 + "<ArticleCount>1</ArticleCount>";
                        strresponse1 = strresponse1 + "<Articles>";
                        strresponse1 = strresponse1 + "<item>";
                        strresponse1 = strresponse1 + " <Title><![CDATA[查询消费记录]]></Title>";
                        strresponse1 = strresponse1 + "<Description><![CDATA[查询消费记录]]></Description>";
                        strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                        strresponse1 = strresponse1 + "<Url><![CDATA[" + a1_url + "]]></Url>";
                        strresponse1 = strresponse1 + "</item>";
                        strresponse1 = strresponse1 + "</Articles>";
                        strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                        strresponse1 = strresponse1 + "</xml>";
                        WriteLog(Request.UserHostAddress.ToString()+"/"+DateTime.Now.ToString()+"/"+ind_id + "/cxxf/");//将操作记录写入文本文件
                        Response.Write(strresponse1);
                    }
                    else if (content.Equals("XXGL") == true || content.Equals("xxgl") == true)
                    {
                        string a2_url = "http://lcsy2008.eicp.net/wbkpro/mobile_info.aspx?ind="+ind_id;//添加能从互联网直接访问的完整的mobile_info路径,query=ind=ind_id
                        string strresponse1 = "<xml>";
                        strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                        strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                        strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                        strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                        strresponse1 = strresponse1 + "<ArticleCount>1</ArticleCount>";
                        strresponse1 = strresponse1 + "<Articles>";
                        strresponse1 = strresponse1 + "<item>";
                        strresponse1 = strresponse1 + " <Title><![CDATA[查询个人信息]]></Title>";
                        strresponse1 = strresponse1 + "<Description><![CDATA[查询个人信息]]></Description>";
                        strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                        strresponse1 = strresponse1 + "<Url><![CDATA[" + a2_url + "]]></Url>";
                        strresponse1 = strresponse1 + "</item>";
                        strresponse1 = strresponse1 + "</Articles>";
                        strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                        strresponse1 = strresponse1 + "</xml>";
                        WriteLog(Request.UserHostAddress.ToString() + "/" + DateTime.Now.ToString() + "/" + ind_id + "/xxgl/");//将操作记录写入文本文件
                        Response.Write(strresponse1);
                    }
                   
                    
                    #endregion
                    else
                    {
                        //不是关键字的普通文字交互，显示搜索结果
                        SqlCommand com_getres = new SqlCommand("select ENTER_CARELINK from INFO_ENTER where ENTER_NAME=@name",sc);
                        com_getres.Parameters.AddWithValue("@name", content);
                        object aa=com_getres.ExecuteScalar();
                        if (aa != null)
                        {
                            
                            string lk = aa.ToString();
                            string lk1 = "http://lcsy2008.eicp.net/wbkpro/mobile_enterinfo.aspx";
                            string strresponse1 = "<xml>";
                            strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                            strresponse1 = strresponse1 + "<ArticleCount>1</ArticleCount>";
                            strresponse1 = strresponse1 + "<Articles>";
                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[搜索结果：]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[" + content + "]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + lk1 + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";
                            strresponse1 = strresponse1 + "</Articles>";
                            strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                            strresponse1 = strresponse1 + "</xml>";
                            WriteLog(Request.UserHostAddress.ToString() + "/" + DateTime.Now.ToString() + "/" + ind_id + "/search/" + content);//将操作记录写入文本文件
                            Response.Write(strresponse1);
                        }
                        else
                        {
                            string strresponse4 = "<xml>";
                            strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
                            strresponse4 = strresponse4 + " <Content><![CDATA[" + "您的搜索词为" + content + ";搜索功能将在后期添加。" + "]]></Content>";
                            
                            strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
                            strresponse4 = strresponse4 + "</xml>";
                            WriteLog(Request.UserHostAddress.ToString() + "/" + DateTime.Now.ToString() + "/" + ind_id + "/searchfail/" + content);//将操作记录写入文本文件
                            Response.Write(strresponse4);
                        }
                    }

                }
                else if (msgtype.Equals("location") == true)
                {
                    string loc_x = xn.SelectSingleNode("//Location_X").InnerText;
                    string loc_y = xn.SelectSingleNode("//Location_Y").InnerText;
                    string loc_info = xn.SelectSingleNode("//Label").InnerText;
                    string strresponse2 = "<xml>";
                    strresponse2 = strresponse2 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                    strresponse2 = strresponse2 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                    strresponse2 = strresponse2 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                    strresponse2 = strresponse2 + "<MsgType><![CDATA[text]]></MsgType>";
                    strresponse2 = strresponse2 + " <Content><![CDATA[" + Url1 + "\n" + loc_info + "\n纬度：" + loc_x + "\n经度：" + loc_y + "\n" + "]]></Content>";
                    strresponse2 = strresponse2 + "<FuncFlag>0</FuncFlag>";
                    strresponse2 = strresponse2 + "</xml>";
                    WriteLog(Request.UserHostAddress.ToString() + "/" + DateTime.Now.ToString() + "/" + ind_id + "/location/" + loc_info + "-" + loc_x + "-" + loc_y);//将操作记录写入文本文件
                    Response.Write(strresponse2);
                }

            }
            else//用户仍未注册
            {
                string reg_url = "http://lcsy2008.eicp.net/wbkpro/mobile_register.aspx?openid="+FromUserName;//querystring:openid(fromusername)---mobile_register
                string strresponse = "<xml>";
                strresponse = strresponse + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                strresponse = strresponse + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                strresponse = strresponse + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                strresponse = strresponse + "<MsgType><![CDATA[news]]></MsgType>";
                strresponse = strresponse + "<ArticleCount>1</ArticleCount>";
                strresponse = strresponse + "<Articles>";
                strresponse = strresponse + "<item>";
                strresponse = strresponse + " <Title><![CDATA[请注册平台用户后继续使用~!]]></Title>";
                strresponse = strresponse + "<Description><![CDATA[请注册平台用户后继续使用~!]]></Description>";
                strresponse = strresponse + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                strresponse = strresponse + "<Url><![CDATA[" + reg_url + "]]></Url>";
                strresponse = strresponse + "</item>";
                strresponse = strresponse + "</Articles>";
                strresponse = strresponse + "<FuncFlag>1</FuncFlag>";
                strresponse = strresponse + "</xml>";
                Response.Write(strresponse);
            }
            sc.Close();
        }
#endregion
        }
        else if (exist(ToUserName)==true) 
        {
            //子平台操作
            #region 子平台操作
            SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc1.Open();
            string sqlstr = "select ENTER_NAME from INFO_ENTER where ENTER_WXACOUNT=@ac";
            SqlCommand com_getname = new SqlCommand(sqlstr, sc1);
            com_getname.Parameters.AddWithValue("@ac", ToUserName);
            string enter_name = com_getname.ExecuteScalar().ToString();
            SqlCommand com_getid = new SqlCommand("select ENTER_ID from INFO_ENTER where ENTER_WXACOUNT=@ac1", sc1);
            com_getid.Parameters.AddWithValue("@ac1", ToUserName);
            string enter_id = com_getid.ExecuteScalar().ToString();

            //先判断是否是关注事件，若是则跳转至授权，若不是则跳过
            if (msgtype.Equals("event") == true)
            {
                string ev = xn.SelectSingleNode("//Event").InnerText;
                string ev_key = xn.SelectSingleNode("//EventKey").InnerText;
                if (ev.Equals("subscribe") == true)
                {
                    #region 关注事件
                    string auth_url = "http://lcsy2008.eicp.net/wbkpro/mobile_authorize.aspx?openid=" + FromUserName + "&enterid=" + enter_id;//添加能从互联网直接访问的完整的mobile_authorize路径
                    string image_url2 = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang3.jpg";
                    string strresponse1 = "<xml>";
                    strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                    strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                    strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                    strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                    strresponse1 = strresponse1 + "<ArticleCount>2</ArticleCount>";
                    strresponse1 = strresponse1 + "<Articles>";
                    strresponse1 = strresponse1 + "<item>";
                    strresponse1 = strresponse1 + " <Title><![CDATA[欢迎关注"+enter_name+"!请选择您的用户身份]]></Title>";
                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" +image_url2+ "]]></PicUrl>";
                    strresponse1 = strresponse1 + "<Url><![CDATA[" + "]]></Url>";
                    strresponse1 = strresponse1 + "</item>";
                    strresponse1 = strresponse1 + "<item>";
                    strresponse1 = strresponse1 + " <Title><![CDATA[点击进入授权页面，选择您的用户身份：注册用户或游客]]></Title>";
                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                    strresponse1 = strresponse1 + "<Url><![CDATA[" + auth_url + "]]></Url>";
                    strresponse1 = strresponse1 + "</item>";
                    strresponse1 = strresponse1 + "</Articles>";
                    strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                    strresponse1 = strresponse1 + "</xml>";

                    Response.Write(strresponse1);
                    #endregion
                }
                else if (ev.Equals("unsubscribe") == true)
                {
                    #region 取消关注事件
                    //删除数据表中所有连接，先判断用户是否已经授权，然后删引用
                    SqlCommand get_id=new SqlCommand("select IND_ID from IND_CARE where ENTER_ID=@eid and IND_OPENID=@oid",sc1);
                    get_id.Parameters.AddWithValue("@eid",enter_id);
                    get_id.Parameters.AddWithValue("@oid",FromUserName);
                    object a=get_id.ExecuteScalar();
                    if(a!=null)//用户在数据库中已存在
                    {
                        string tmp_indid=a.ToString();
                        SqlCommand com_delcare = new SqlCommand("delete from IND_CARE where IND_ID=@id and ENTER_ID=@eidd",sc1);
                        com_delcare.Parameters.AddWithValue("@id",tmp_indid);
                        com_delcare.Parameters.AddWithValue("@eidd",enter_id);
                        com_delcare.ExecuteNonQuery();
                        sc1.Close();
                        //后续添加删除更多连接的数据库操作
                    }
                    #endregion
                }
                else if (ev.Equals("CLICK") == true)
                {
                    //点击事件，先判断用户身份
                    SqlCommand com_check = new SqlCommand();
                    com_check.CommandText = "select IND_ID from IND_CARE where ENTER_ID=@eid1 and IND_OPENID=@a";
                    com_check.Connection = sc1;
                    com_check.Parameters.AddWithValue("@eid1", enter_id);
                    com_check.Parameters.AddWithValue("@a", FromUserName);
                    object a = com_check.ExecuteScalar();
                    SqlCommand com_checkvis = new SqlCommand();
                    com_checkvis.CommandText = "select ENTER_ID from INFO_VISITER where VISITER_ID=@id and ENTER_ID=@idd";
                    com_checkvis.Connection = sc1;
                    com_checkvis.Parameters.AddWithValue("@id", FromUserName);
                    com_checkvis.Parameters.AddWithValue("@idd", enter_id);
                    object b = com_checkvis.ExecuteScalar();
                    if (a != null && b == null)//已授权，即IND_CARE表中已存在记录,已授权即默认为正式用户
                    {
                        string ind_id = a.ToString();
                        //根据ev_key判断事件代码
                        if (ev_key.Equals("DISC_INFO") == true)
                        {
                            #region 优惠事件
                            DateTime now = DateTime.Now;
                            string start_now = now.Year + "-" + now.Month + "-" + now.Day + " " + now.Hour + ":" + now.Minute + ":" + now.Second;
                            string end_now = start_now;
                            string[] d_id = new string[10];
                            string[] d_title = new string[10];
                            string[] d_image = new string[10];
                            try
                            {
                                SqlCommand get_discount = new SqlCommand();
                                get_discount.Connection = sc1;
                                get_discount.CommandText = "select DISCOUNT_ID,DISCOUNT_TITLE,DISCOUNT_IMAGE from INFO_DISCOUNT where ENTER_ID=@did and  DISCOUNT_START<convert(smalldatetime,@start) and DISCOUNT_END>convert(smalldatetime,@end)";
                                get_discount.Parameters.AddWithValue("@did", enter_id);
                                get_discount.Parameters.AddWithValue("@start", start_now);
                                get_discount.Parameters.AddWithValue("@end", end_now);
                               
                                SqlDataReader r = get_discount.ExecuteReader();
                                int count_d = 0;
                                while (r.Read())
                                {
                                    d_id[count_d] = r.GetString(r.GetOrdinal("DISCOUNT_ID"));
                                    d_title[count_d] = r.GetString(r.GetOrdinal("DISCOUNT_TITLE"));
                                    d_image[count_d] = r.GetString(r.GetOrdinal("DISCOUNT_IMAGE"));
                                    count_d++;
                                }
                                r.Close();
                                
                                if (count_d == 0)
                                {
                                    //没有在线的优惠，返回文本提示
                                    string strresponse4 = "<xml>";
                                    strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                    strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                    strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                    strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
                                    strresponse4 = strresponse4 + "<Content><![CDATA[当前没有在线的活动]]></Content>";
                                    strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
                                    strresponse4 = strresponse4 + "</xml>";
                                    Response.Write(strresponse4);
                                }
                                else
                                {
                                    //有在线的活动
                                    
                                    string strresponse1 = "<xml>";
                                    strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                    strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                    strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                    strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                                    strresponse1 = strresponse1 + "<ArticleCount>" + count_d+ "</ArticleCount>";
                                    strresponse1 = strresponse1 + "<Articles>";
                                    for (int i = 0; i < count_d; i++)
                                    {
                                        string d_image_tmp = "http:/" + "/lcsy2008.eicp.net/wbkpro" + d_image[i].Remove(0, 1);
                                        string d_url = "http://lcsy2008.eicp.net/wbkpro/mobile_discount.aspx?did=" + d_id[i]+"&indid="+ind_id;
                                        strresponse1 = strresponse1 + "<item>";
                                        strresponse1 = strresponse1 + "<Title><![CDATA[" + d_title[i] + "]]></Title>";
                                        strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                        strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + d_image_tmp + "]]></PicUrl>";
                                        strresponse1 = strresponse1 + "<Url><![CDATA[" + d_url + "]]></Url>";
                                        strresponse1 = strresponse1 + "</item>";
                                    }
                                    strresponse1 = strresponse1 + "</Articles>";
                                    strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                                    strresponse1 = strresponse1 + "</xml>";
                                    Response.Write(strresponse1);
                                }
                            }

                            catch (Exception d_e)
                            {
                                string strresponse4 = "<xml>";
                                strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
                                strresponse4 = strresponse4 + "<Content><![CDATA[" + d_e.Message + d_e.StackTrace + "]]></Content>";
                                strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
                                strresponse4 = strresponse4 + "</xml>";
                                Response.Write(strresponse4);
                            }
                            #endregion
                        }
                        else if (ev_key.Equals("USER_INFO") == true)
                        {
                            #region 信息管理
                            string a1_url = "http://lcsy2008.eicp.net/wbkpro/mobile_xiaofeijl.aspx?ind=" + ind_id;//添加能从互联网直接访问的完整的mobile_xiaofeijl路径,query=ind=ind_id
                            string a2_url = "http://lcsy2008.eicp.net/wbkpro/mobile_info.aspx?ind=" + ind_id;//添加能从互联网直接访问的完整的mobile_info路径,query=ind=ind_id
                            string a3_url = "http://lcsy2008.eicp.net/wbkpro/mobile_invite.aspx?openid=" + FromUserName + "&enterid=" + enter_id;//添加能从互联网直接访问的完整的mobile_invite路径，query中加上openid=fromusername,enterid=enter_id
                            string a4_url = "http://lcsy2008.eicp.net/wbkpro/mobile_mydiscount.aspx?indid="+ind_id;
                            string a5_url = "http://lcsy2008.eicp.net/wbkpro/mobile_mylottery.aspx?indid=" + ind_id;
                            string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang3.jpg";
                            string strresponse1 = "<xml>";
                            strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                            strresponse1 = strresponse1 + "<ArticleCount>6</ArticleCount>";
                            strresponse1 = strresponse1 + "<Articles>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[会员特权]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + image_url + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[我的信息]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + a2_url + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[我的消费]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + a1_url + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[我的优惠]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + a4_url + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[我的抽奖]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + a5_url + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[我的邀请]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + a3_url + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";
                            strresponse1 = strresponse1 + "</Articles>";
                            strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                            strresponse1 = strresponse1 + "</xml>";
                            Response.Write(strresponse1);
                            #endregion
                        }
                        else if (ev_key.Equals("HG_INFO") == true)
                        {
                            #region 换购
                            string[] pid = new string[10];
                            string[] pname = new string[10];
                            int[] psum = new int[10];
                            try
                            {
                                SqlCommand com_gethg = new SqlCommand();
                                com_gethg.Connection = sc1;
                                com_gethg.CommandText = "select INFO_PRODUCT.PRODUCT_ID,INFO_PRODUCT.PRODUCT_NAME,INFO_EXCHANGE.EX_SUM from INFO_PRODUCT,INFO_EXCHANGE where INFO_EXCHANGE.PRODUCT_ID=INFO_PRODUCT.PRODUCT_ID and INFO_PRODUCT.PRODUCT_ID=(select INFO_EXCHANGE.PRODUCT_ID from INFO_EXCHANGE,INFO_PRODUCT where INFO_PRODUCT.ENTER_ID=@eid2 and INFO_EXCHANGE.PRODUCT_ID=INFO_PRODUCT.PRODUCT_ID)";
                                com_gethg.Parameters.AddWithValue("@eid2", enter_id);
                                SqlDataReader re = com_gethg.ExecuteReader();
                                int i = 0;
                                while (re.Read())
                                {
                                    pid[i] = re.GetString(re.GetOrdinal("PRODUCT_ID"));
                                    pname[i] = re.GetString(re.GetOrdinal("PRODUCT_NAME"));
                                    psum[i] = re.GetInt32(re.GetOrdinal("EX_SUM"));
                                    i++;
                                }
                                int count = i + 1;
                                string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang.jpg";
                                string strresponse1 = "<xml>";
                                strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                                strresponse1 = strresponse1 + "<ArticleCount>" + count + "</ArticleCount>";
                                strresponse1 = strresponse1 + "<Articles>";

                                strresponse1 = strresponse1 + "<item>";
                                //图片导航条信息
                                strresponse1 = strresponse1 + " <Title><![CDATA[换购商品有" + (count - 1) + "件]]></Title>";
                                strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + image_url + "]]></PicUrl>";
                                strresponse1 = strresponse1 + "<Url><![CDATA[]]></Url>";
                                strresponse1 = strresponse1 + "</item>";

                                for (int o = 0; o < count - 1; o++)
                                {
                                    string a3_url = "http://lcsy2008.eicp.net/wbkpro/mobile_hg.aspx?pid=" + pid[o] + "&openid=" + FromUserName + "&enterid=" + enter_id;
                                    strresponse1 = strresponse1 + "<item>";
                                    strresponse1 = strresponse1 + " <Title><![CDATA[" + pname[o] + "余量还有:"+psum[o]+"]]></Title>";
                                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                                    strresponse1 = strresponse1 + "<Url><![CDATA[" + a3_url + "]]></Url>";
                                    strresponse1 = strresponse1 + "</item>";
                                }
                                strresponse1 = strresponse1 + "</Articles>";
                                strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                                strresponse1 = strresponse1 + "</xml>";
                                Response.Write(strresponse1);



                            }
                            catch (Exception e)
                            {
                                string strresponse2 = "<xml>";
                                strresponse2 = strresponse2 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse2 = strresponse2 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse2 = strresponse2 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse2 = strresponse2 + "<MsgType><![CDATA[text]]></MsgType>";
                                strresponse2 = strresponse2 + " <Content><![CDATA[" + e.Message + e.StackTrace + "]]></Content>";
                                strresponse2 = strresponse2 + "<FuncFlag>0</FuncFlag>";
                                strresponse2 = strresponse2 + "</xml>";
                                Response.Write(strresponse2);
                            }
                            #endregion
                        }
                        else if (ev_key.Equals("CONTACT_INFO") == true)
                        {
                            #region 平台联系信息
                            string name = "";
                            string phone = "";
                            string qq = "";
                            string sqlstr1 = "select ENTER_NAME,ENTER_PHONE,ENTER_QQ from INFO_ENTER where ENTER_WXACOUNT=@ac";
                            SqlCommand com_getcontact = new SqlCommand(sqlstr1, sc1);
                            com_getcontact.Parameters.AddWithValue("@ac", ToUserName);
                            SqlDataReader r = com_getcontact.ExecuteReader();
                            while(r.Read())
                            {
                                name = r.GetString(r.GetOrdinal("ENTER_NAME"));
                                phone = r.GetString(r.GetOrdinal("ENTER_PHONE"));
                                qq = r.GetString(r.GetOrdinal("ENTER_QQ"));
                            }
                            string contactinfo = "企业名称： " + name + "\n" + "企业负责人联系电话： " + phone +"\n"+ "企业客服QQ： " + qq;
                            string strresponse4 = "<xml>";
                            strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
                            strresponse4 = strresponse4 + " <Content><![CDATA[" +contactinfo  + "]]></Content>";
                            strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
                            strresponse4 = strresponse4 + "</xml>";
                            Response.Write(strresponse4);
                            #endregion
                        }
                        else if (ev_key.Equals("LOTTERY_INFO") == true)
                        {
                            #region 大转盘
                            try
                            {
                                DateTime now = DateTime.Now;
                                string start_now = now.Year + "-" + now.Month + "-" + now.Day + " " + now.Hour + ":" + now.Minute + ":" + now.Second;
                                string end_now = start_now;
                                SqlCommand get_lottery = new SqlCommand();
                                get_lottery.Connection = sc1;
                                get_lottery.CommandText = "select count(LOTTERY_ID) from INFO_LOTTERY where ENTER_ID=@did and LOTTERY_START<convert(smalldatetime,@start) and LOTTERY_END>convert(smalldatetime,@end)";
                                get_lottery.Parameters.AddWithValue("@did", enter_id);
                                get_lottery.Parameters.AddWithValue("@start", start_now);
                                get_lottery.Parameters.AddWithValue("@end", end_now);
                                int count = Convert.ToInt32(get_lottery.ExecuteScalar().ToString());
                                if (count == 0)
                                {
                                    string strresponse4 = "<xml>";
                                    strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                    strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                    strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                    strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
                                    strresponse4 = strresponse4 + " <Content><![CDATA[无在线活动！]]></Content>";
                                    strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
                                    strresponse4 = strresponse4 + "</xml>";
                                    Response.Write(strresponse4);
                                }
                                else
                                {
                                    SqlCommand get_image = new SqlCommand("select LOTTERY_ID,LOTTERY_IMAGE from INFO_LOTTERY where ENTER_ID=@did1 and LOTTERY_START<convert(smalldatetime,@start1) and LOTTERY_END>convert(smalldatetime,@end1) and FPRIZE_NUM>0 and SPRIZE_NUM>0 and TPRIZE_NUM>0",sc1);
                                    get_image.Parameters.AddWithValue("@did1", enter_id);
                                    get_image.Parameters.AddWithValue("@start1", start_now);
                                    get_image.Parameters.AddWithValue("@end1", end_now);
                                    SqlDataReader ss = get_image.ExecuteReader();
                                    string path = "";
                                    string lid = "";
                                    while (ss.Read())
                                    {
                                        lid = ss.GetString(ss.GetOrdinal("LOTTERY_ID"));
                                        path = ss.GetString(ss.GetOrdinal("LOTTERY_IMAGE"));
                                    }
                                    ss.Close();
                                    string d_image_tmp = "http:/" + "/lcsy2008.eicp.net/wbkpro" + path.Remove(0, 1);
                                    string d_url = "http://lcsy2008.eicp.net/wbkpro/mobile_lottery.aspx?eid=" + enter_id + "&indid=" + ind_id+"&lid="+lid;
                                    string strresponse1 = "<xml>";
                                    strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                    strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                    strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                    strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                                    strresponse1 = strresponse1 + "<ArticleCount>1</ArticleCount>";
                                    strresponse1 = strresponse1 + "<Articles>";
                                    strresponse1 = strresponse1 + "<item>";
                                    strresponse1 = strresponse1 + "<Title><![CDATA[点击进入抽奖活动]]></Title>";
                                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + d_image_tmp + "]]></PicUrl>";
                                    strresponse1 = strresponse1 + "<Url><![CDATA[" + d_url + "]]></Url>";
                                    strresponse1 = strresponse1 + "</item>";
                                    strresponse1 = strresponse1 + "</Articles>";
                                    strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                                    strresponse1 = strresponse1 + "</xml>";
                                    Response.Write(strresponse1);
                                }
                            }
                            catch (Exception ex_un)
                            {
                                string strresponse4 = "<xml>";
                                strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
                                strresponse4 = strresponse4 + " <Content><![CDATA["+ex_un.Message+ex_un.StackTrace+"]]></Content>";
                                strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
                                strresponse4 = strresponse4 + "</xml>";
                                Response.Write(strresponse4);
                            }
                            #endregion
                        }
                        else if (ev_key.Equals("SPECIAL_INFO") == true)
                        {
                            #region 精选产品
                            string[] pid = new string[10];
                            string[] pname = new string[10];
                            try
                            {
                                SqlCommand com_getsp = new SqlCommand();
                                com_getsp.Connection = sc1;
                                com_getsp.CommandText = "select PRODUCT_ID,PRODUCT_NAME from INFO_PRODUCT where ENTER_ID=@eid3 and SPECIAL=@l";
                                com_getsp.Parameters.AddWithValue("@eid3",enter_id);
                                com_getsp.Parameters.AddWithValue("@l","TRUE");
                                SqlDataReader re = com_getsp.ExecuteReader();
                                int i = 0;
                                while (re.Read())
                                {
                                    pid[i] = re.GetString(re.GetOrdinal("PRODUCT_ID"));
                                    pname[i] = re.GetString(re.GetOrdinal("PRODUCT_NAME"));
                                    i++;
                                }
                                int count = i + 1;
                                string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang.jpg";
                                string strresponse1 = "<xml>";
                                strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                                strresponse1 = strresponse1 + "<ArticleCount>" + count + "</ArticleCount>";
                                strresponse1 = strresponse1 + "<Articles>";

                                strresponse1 = strresponse1 + "<item>";
                                //图片导航条信息
                                strresponse1 = strresponse1 + " <Title><![CDATA[精选商品有" + (count - 1) + "件]]></Title>";
                                strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + image_url + "]]></PicUrl>";
                                strresponse1 = strresponse1 + "<Url><![CDATA[]]></Url>";
                                strresponse1 = strresponse1 + "</item>";

                                for (int o = 0; o < count - 1; o++)
                                {
                                    string a3_url = "http://lcsy2008.eicp.net/wbkpro/mobile_product.aspx?pid=" + pid[o];
                                    strresponse1 = strresponse1 + "<item>";
                                    strresponse1 = strresponse1 + " <Title><![CDATA[" + pname[o] + "]]></Title>";
                                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                                    strresponse1 = strresponse1 + "<Url><![CDATA[" + a3_url + "]]></Url>";
                                    strresponse1 = strresponse1 + "</item>";
                                }
                                strresponse1 = strresponse1 + "</Articles>";
                                strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                                strresponse1 = strresponse1 + "</xml>";
                                Response.Write(strresponse1);
                            }
                            catch (Exception sp_e)
                            {
                                string strresponse2 = "<xml>";
                                strresponse2 = strresponse2 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse2 = strresponse2 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse2 = strresponse2 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse2 = strresponse2 + "<MsgType><![CDATA[text]]></MsgType>";
                                strresponse2 = strresponse2 + " <Content><![CDATA[" + sp_e.Message + sp_e.StackTrace + "]]></Content>";
                                strresponse2 = strresponse2 + "<FuncFlag>0</FuncFlag>";
                                strresponse2 = strresponse2 + "</xml>";
                                Response.Write(strresponse2);
                            }
                            #endregion
                        }
                       
                        else
                        {
                            string strresponse4 = "<xml>";
                            strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
                            strresponse4 = strresponse4 + " <Content><![CDATA[" + ev_key + "]]></Content>";
                            strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
                            strresponse4 = strresponse4 + "</xml>";
                            Response.Write(strresponse4);
                        }
                    }
                    else if (b != null && a == null)//访客身份
                    {
                        if (ev_key.Equals("CONTACT_INFO") == true)
                        {
                            #region 平台联系信息
                            string name = "";
                            string phone = "";
                            string qq = "";
                            string sqlstr1 = "select ENTER_NAME,ENTER_PHONE,ENTER_QQ from INFO_ENTER where ENTER_WXACOUNT=@ac";
                            SqlCommand com_getcontact = new SqlCommand(sqlstr1, sc1);
                            com_getcontact.Parameters.AddWithValue("@ac", ToUserName);
                            SqlDataReader r = com_getcontact.ExecuteReader();
                            while (r.Read())
                            {
                                name = r.GetString(r.GetOrdinal("ENTER_NAME"));
                                phone = r.GetString(r.GetOrdinal("ENTER_PHONE"));
                                qq = r.GetString(r.GetOrdinal("ENTER_QQ"));
                            }
                            string contactinfo = "企业名称： " + name + "\n" + "企业负责人联系电话： " + phone + "\n" + "企业客服QQ： " + qq;
                            string strresponse4 = "<xml>";
                            strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
                            strresponse4 = strresponse4 + " <Content><![CDATA[" + contactinfo + "]]></Content>";
                            strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
                            strresponse4 = strresponse4 + "</xml>";
                            Response.Write(strresponse4);
                            #endregion
                        }
                        else if (ev_key.Equals("SPECIAL_INFO") == true)
                        {
                            #region 精选产品
                            string[] pid = new string[10];
                            string[] pname = new string[10];
                            try
                            {
                                SqlCommand com_getsp = new SqlCommand();
                                com_getsp.Connection = sc1;
                                com_getsp.CommandText = "select PRODUCT_ID,PRODUCT_NAME from INFO_PRODUCT where ENTER_ID=@eid4 and SPECIAL=@l";
                                com_getsp.Parameters.AddWithValue("@eid4",enter_id);
                                com_getsp.Parameters.AddWithValue("@l", "TRUE");
                                SqlDataReader re = com_getsp.ExecuteReader();
                                int i = 0;
                                while (re.Read())
                                {
                                    pid[i] = re.GetString(re.GetOrdinal("PRODUCT_ID"));
                                    pname[i] = re.GetString(re.GetOrdinal("PRODUCT_NAME"));
                                    i++;
                                }
                                int count = i + 1;
                                string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang.jpg";
                                string strresponse1 = "<xml>";
                                strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                                strresponse1 = strresponse1 + "<ArticleCount>" + count + "</ArticleCount>";
                                strresponse1 = strresponse1 + "<Articles>";

                                strresponse1 = strresponse1 + "<item>";
                                //图片导航条信息
                                strresponse1 = strresponse1 + " <Title><![CDATA[精选商品有" + (count - 1) + "件]]></Title>";
                                strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + image_url + "]]></PicUrl>";
                                strresponse1 = strresponse1 + "<Url><![CDATA[]]></Url>";
                                strresponse1 = strresponse1 + "</item>";

                                for (int o = 0; o < count - 1; o++)
                                {
                                    string a3_url = "http://lcsy2008.eicp.net/wbkpro/mobile_product.aspx?pid=" + pid[o];
                                    strresponse1 = strresponse1 + "<item>";
                                    strresponse1 = strresponse1 + " <Title><![CDATA[" + pname[o] + "]]></Title>";
                                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                                    strresponse1 = strresponse1 + "<Url><![CDATA[" + a3_url + "]]></Url>";
                                    strresponse1 = strresponse1 + "</item>";
                                }
                                strresponse1 = strresponse1 + "</Articles>";
                                strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                                strresponse1 = strresponse1 + "</xml>";
                                Response.Write(strresponse1);
                            }
                            catch (Exception sp_e)
                            {
                                string strresponse2 = "<xml>";
                                strresponse2 = strresponse2 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse2 = strresponse2 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse2 = strresponse2 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse2 = strresponse2 + "<MsgType><![CDATA[text]]></MsgType>";
                                strresponse2 = strresponse2 + " <Content><![CDATA[" + sp_e.Message + sp_e.StackTrace + "]]></Content>";
                                strresponse2 = strresponse2 + "<FuncFlag>0</FuncFlag>";
                                strresponse2 = strresponse2 + "</xml>";
                                Response.Write(strresponse2);
                            }
                            #endregion
                        }
                        else
                        {
                            string auth_url = "http://lcsy2008.eicp.net/wbkpro/mobile_authorize.aspx?openid=" + FromUserName + "&enterid=" + enter_id;
                            string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang2.jpg";
                            string strresponse1 = "<xml>";
                            strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                            strresponse1 = strresponse1 + "<ArticleCount>2</ArticleCount>";
                            strresponse1 = strresponse1 + "<Articles>";
                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[抱歉，您还不是注册授权用户]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + image_url + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[想享受多种跨平台账号服务和更多积分优惠，请火速关注我们的主平台并注册成为我们的用户，即刻开启畅想之旅~！]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + auth_url + "]]></Url>";//添加主平台关注链接
                            strresponse1 = strresponse1 + "</item>";
                            strresponse1 = strresponse1 + "</Articles>";
                            strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                            strresponse1 = strresponse1 + "</xml>";
                            Response.Write(strresponse1);
                        }
                    }
                        //用户仍未授权
                    else
                    {
                        string auth_url = "http://lcsy2008.eicp.net/wbkpro/mobile_authorize.aspx?openid=" + FromUserName + "&enterid=" + enter_id;//添加能从互联网直接访问的完整的mobile_authorize路径
                        string strresponse1 = "<xml>";
                        strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                        strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                        strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                        strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                        strresponse1 = strresponse1 + "<ArticleCount>1</ArticleCount>";
                        strresponse1 = strresponse1 + "<Articles>";
                        strresponse1 = strresponse1 + "<item>";
                        strresponse1 = strresponse1 + " <Title><![CDATA[是否授权" + enter_name + "获取您的部分个人信息？]]></Title>";
                        strresponse1 = strresponse1 + "<Description><![CDATA[是否授权" + enter_name + "获取您的部分个人信息？]]></Description>";
                        strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                        strresponse1 = strresponse1 + "<Url><![CDATA[" + auth_url + "]]></Url>";
                        strresponse1 = strresponse1 + "</item>";
                        strresponse1 = strresponse1 + "</Articles>";
                        strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                        strresponse1 = strresponse1 + "</xml>";

                        Response.Write(strresponse1);
                    }

                }
                
                //自定义菜单事件处理在此添加

            }
            //不是关注事件则判断用户是否已授权，即IND_CARE中是否存在该关系，利用ENTER_ID和用户的openid检验
            else
            {
                SqlCommand com_check = new SqlCommand();
                com_check.CommandText = "select IND_ID from IND_CARE where ENTER_ID=@eid5 and IND_OPENID=@a";
                com_check.Connection = sc1;
                com_check.Parameters.AddWithValue("@eid5", enter_id);
                com_check.Parameters.AddWithValue("@a", FromUserName);
                object a = com_check.ExecuteScalar();
                SqlCommand com_checkvis = new SqlCommand();
                com_checkvis.CommandText = "select ENTER_ID from INFO_VISITER where VISITER_ID=@id and ENTER_ID=@idd";
                com_checkvis.Connection = sc1;
                com_checkvis.Parameters.AddWithValue("@id",FromUserName);
                com_checkvis.Parameters.AddWithValue("@idd",enter_id);
                object b = com_checkvis.ExecuteScalar();
                if (a != null &&b==null)//已授权，即IND_CARE表中已存在记录,已授权即默认为正式用户
                {

                    string ind_id = a.ToString();

                    if (msgtype.Equals("text") == true)//文本信息处理
                    {
                        string content = xn.SelectSingleNode("//Content").InnerText;

                        #region 关键字判断

                        if (content.Equals("CXXF") == true || content.Equals("cxxf") == true)
                        {
                            string a1_url = "http://lcsy2008.eicp.net/wbkpro/mobile_xiaofeijl.aspx?ind=" + ind_id;//添加能从互联网直接访问的完整的mobile_xiaofeijl路径,query=ind=ind_id
                            string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang3.jpg";
                            string strresponse1 = "<xml>";
                            strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                            strresponse1 = strresponse1 + "<ArticleCount>1</ArticleCount>";
                            strresponse1 = strresponse1 + "<Articles>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[查询消费记录]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" +image_url+ "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + a1_url + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "</Articles>";
                            strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                            strresponse1 = strresponse1 + "</xml>";
                            Response.Write(strresponse1);
                        }
                        else if (content.Equals("WDYQ") == true || content.Equals("wdyq") == true)
                        {
                            string a3_url = "http://lcsy2008.eicp.net/wbkpro/mobile_invite.aspx?openid=" + FromUserName + "&enterid=" + enter_id;//添加能从互联网直接访问的完整的mobile_invite路径，query中加上openid=fromusername,enterid=enter_id
                            string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang3.jpg";
                            string strresponse1 = "<xml>";
                            strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                            strresponse1 = strresponse1 + "<ArticleCount>1</ArticleCount>";
                            strresponse1 = strresponse1 + "<Articles>";
                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[我的邀请]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" +image_url+ "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + a3_url + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";
                            strresponse1 = strresponse1 + "</Articles>";
                            strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                            strresponse1 = strresponse1 + "</xml>";
                            Response.Write(strresponse1);
                        }
                        else if (content.Equals("XXGL") == true || content.Equals("xxgl") == true)
                        {
                            string a2_url = "http://lcsy2008.eicp.net/wbkpro/mobile_info.aspx?ind=" + ind_id;//添加能从互联网直接访问的完整的mobile_info路径,query=ind=ind_id
                            string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang.jpg";
                            string strresponse1 = "<xml>";
                            strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                            strresponse1 = strresponse1 + "<ArticleCount>2</ArticleCount>";
                            strresponse1 = strresponse1 + "<Articles>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[个人信息-查看积分-修改邮箱]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA["+image_url + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[点击跳转查询个人信息]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + a2_url + "]]></Url>";
                            strresponse1 = strresponse1 + "</item>";
                            strresponse1 = strresponse1 + "</Articles>";
                            strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                            strresponse1 = strresponse1 + "</xml>";
                            Response.Write(strresponse1);
                        }
                        else if (content.Equals("WYHG") == true || content.Equals("wyhg") == true)
                        {
                            string[] pid = new string[10];
                            string[] pname = new string[10];
                            int[] psum = new int[10];
                            try
                            {
                                SqlCommand com_gethg = new SqlCommand();
                                com_gethg.Connection = sc1;
                                com_gethg.CommandText = "select INFO_PRODUCT.PRODUCT_ID,INFO_PRODUCT.PRODUCT_NAME,INFO_EXCHANGE.EX_SUM from INFO_PRODUCT,INFO_EXCHANGE where INFO_EXCHANGE.PRODUCT_ID=INFO_PRODUCT.PRODUCT_ID and INFO_PRODUCT.PRODUCT_ID=(select INFO_EXCHANGE.PRODUCT_ID from INFO_EXCHANGE,INFO_PRODUCT where INFO_PRODUCT.ENTER_ID=@eid6 and INFO_EXCHANGE.PRODUCT_ID=INFO_PRODUCT.PRODUCT_ID)";
                                com_gethg.Parameters.AddWithValue("@eid6", enter_id);
                                SqlDataReader re = com_gethg.ExecuteReader();
                                int i = 0;
                                while (re.Read())
                                {
                                    pid[i] = re.GetString(re.GetOrdinal("PRODUCT_ID"));
                                    pname[i] = re.GetString(re.GetOrdinal("PRODUCT_NAME"));
                                    psum[i] = re.GetInt32(re.GetOrdinal("EX_SUM"));
                                    i++;
                                }
                                int count = i + 1;
                                string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang.jpg";
                                string strresponse1 = "<xml>";
                                strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                                strresponse1 = strresponse1 + "<ArticleCount>" + count + "</ArticleCount>";
                                strresponse1 = strresponse1 + "<Articles>";

                                strresponse1 = strresponse1 + "<item>";
                                //图片导航条信息
                                strresponse1 = strresponse1 + " <Title><![CDATA[换购商品有" + (count - 1) + "件]]></Title>";
                                strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + image_url + "]]></PicUrl>";
                                strresponse1 = strresponse1 + "<Url><![CDATA[]]></Url>";
                                strresponse1 = strresponse1 + "</item>";

                                for (int o = 0; o < count - 1; o++)
                                {
                                    string a3_url = "http://lcsy2008.eicp.net/wbkpro/mobile_hg.aspx?pid=" + pid[o] + "&openid=" + FromUserName + "&enterid=" + enter_id;
                                    strresponse1 = strresponse1 + "<item>";
                                    strresponse1 = strresponse1 + " <Title><![CDATA[" + pname[o] + "余量还有:"+psum[o]+"]]></Title>";
                                    strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                                    strresponse1 = strresponse1 + "<Url><![CDATA[" + a3_url + "]]></Url>";
                                    strresponse1 = strresponse1 + "</item>";
                                }
                                strresponse1 = strresponse1 + "</Articles>";
                                strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                                strresponse1 = strresponse1 + "</xml>";
                                Response.Write(strresponse1);



                            }
                            catch(Exception e)
                            {
                                string strresponse2 = "<xml>";
                                strresponse2 = strresponse2 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse2 = strresponse2 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse2 = strresponse2 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse2 = strresponse2 + "<MsgType><![CDATA[text]]></MsgType>";
                                strresponse2 = strresponse2 + " <Content><![CDATA["+e.Message+e.StackTrace+ "]]></Content>";
                                strresponse2 = strresponse2 + "<FuncFlag>0</FuncFlag>";
                                strresponse2 = strresponse2 + "</xml>";
                                Response.Write(strresponse2);
                            }
                        }
                            

                        #endregion

                        else
                        {
                            //不是关键字的普通文字交互，显示搜索结果
                            #region 词条搜索
                            List<string> content1 = search(content, ToUserName);
                            int count = content1.Count + 1;
                            string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang.jpg";
                            //content1[0].Insert(content1[0].Length, count.ToString());
                            if (content1[0].Equals("没有搜索结果!") == false && content1[0].Equals("您输入的信息格式无法识别!") == false)
                            {
                                string strresponse = "<xml>";
                                strresponse = strresponse + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse = strresponse + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse = strresponse + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse = strresponse + "<MsgType><![CDATA[news]]></MsgType>";
                                strresponse = strresponse + "<ArticleCount>" + count.ToString() + "</ArticleCount>";
                                strresponse = strresponse + "<Articles>";

                                strresponse = strresponse + "<item>";
                                strresponse = strresponse + " <Title><![CDATA[您的搜索内容" + content + "得到了" + content1.Count.ToString() + "条搜索结果，我们抽取了前五名结果返回给您，您可以点击以下链接进入详细信息查看页面]]></Title>";
                                strresponse = strresponse + "<Description><![CDATA[]]></Description>";
                                strresponse = strresponse + "<PicUrl><![CDATA[" + image_url + "]]></PicUrl>";
                                strresponse = strresponse + "<Url><![CDATA[" + "]]></Url>";
                                strresponse = strresponse + "</item>";
                                for (int i = 0; i < count - 1; i++)
                                {
                                    string pid = content1[i].Substring(9, 6);
                                    content1[i] = content1[i].Remove(0, 15);
                                    //string image_url = "http:/"+"/lcsy2008.eicp.net/wbkpro/product_image/" + pid + "_image.jpg";
                                    string url = "http://lcsy2008.eicp.net/wbkpro/mobile_product.aspx?pid=" + pid;
                                    strresponse = strresponse + "<item>";
                                    strresponse = strresponse + " <Title><![CDATA[" + content1[i] + "]]></Title>";
                                    strresponse = strresponse + "<Description><![CDATA[]]></Description>";
                                    strresponse = strresponse + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                                    strresponse = strresponse + "<Url><![CDATA[" + url + "]]></Url>";
                                    strresponse = strresponse + "</item>";
                                }
                                strresponse = strresponse + "</Articles>";
                                strresponse = strresponse + "<FuncFlag>1</FuncFlag>";
                                strresponse = strresponse + "</xml>";
                                //WriteLog(FromUserName + " " + content + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString());
                                //后期记录用户数据
                                Response.Write(strresponse);
                            }
                            else
                            {
                                string strresponse1 = "<xml>";
                                strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse1 = strresponse1 + "<MsgType><![CDATA[text]]></MsgType>";
                                if (content1[0].Equals("您输入的信息格式无法识别!") == true)
                                    strresponse1 = strresponse1 + " <Content><![CDATA[" + content1[0] + content1[1] + "]]></Content>";
                                else
                                    strresponse1 = strresponse1 + " <Content><![CDATA[" + content1[0] + "]]></Content>";
                                strresponse1 = strresponse1 + "<FuncFlag>0</FuncFlag>";
                                strresponse1 = strresponse1 + "</xml>";
                                //WriteLog(FromUserName + " " + content + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString());
                                Response.Write(strresponse1);
                            }
                            #endregion
                        }
                       
                    }
                    else if (msgtype.Equals("location") == true)
                    {
                        string loc_x = xn.SelectSingleNode("//Location_X").InnerText;
                        string loc_y = xn.SelectSingleNode("//Location_Y").InnerText;
                        string loc_info = xn.SelectSingleNode("//Label").InnerText;
                        string strresponse2 = "<xml>";
                        strresponse2 = strresponse2 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                        strresponse2 = strresponse2 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                        strresponse2 = strresponse2 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                        strresponse2 = strresponse2 + "<MsgType><![CDATA[text]]></MsgType>";
                        strresponse2 = strresponse2 + " <Content><![CDATA[" + Url1 + "\n" + loc_info + "\n纬度：" + loc_x + "\n经度：" + loc_y + "\n" + "]]></Content>";
                        strresponse2 = strresponse2 + "<FuncFlag>0</FuncFlag>";
                        strresponse2 = strresponse2 + "</xml>";
                        Response.Write(strresponse2);
                    }

                 }
                else if (b != null && a == null)
                {
                    //访客操作
                    if (msgtype.Equals("text") == true)//文本信息处理
                    {
                        string content = xn.SelectSingleNode("//Content").InnerText;

                        #region 关键字判断2

                        if (content.Equals("CXXF") == true || content.Equals("cxxf") == true || content.Equals("WYSRYQM") == true || content.Equals("wysryqm") == true || content.Equals("XXGL") == true || content.Equals("xxgl") == true || content.Equals("WYYQ") == true || content.Equals("wyyq") == true || content.Equals("WYHG") == true || content.Equals("wyhg") == true)
                        {
                            //访客输入系统指令时，返回用户提示信息和主平台关注链接
                            //string ll = "weixin://addfriend/test1992";
                            string auth_url = "http://lcsy2008.eicp.net/wbkpro/mobile_authorize.aspx?openid=" + FromUserName + "&enterid=" + enter_id;
                            string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang2.jpg";
                            string strresponse1 = "<xml>";
                            strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                            strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                            strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                            strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                            strresponse1 = strresponse1 + "<ArticleCount>2</ArticleCount>";
                            strresponse1 = strresponse1 + "<Articles>";
                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[抱歉，您还不是注册授权用户]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + image_url+"]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[]]></Url>";
                            strresponse1 = strresponse1 + "</item>";

                            strresponse1 = strresponse1 + "<item>";
                            strresponse1 = strresponse1 + " <Title><![CDATA[想享受多种跨平台账号服务和更多积分优惠，请火速关注我们的主平台并注册成为我们的用户，即刻开启畅想之旅~！]]></Title>";
                            strresponse1 = strresponse1 + "<Description><![CDATA[]]></Description>";
                            strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                            strresponse1 = strresponse1 + "<Url><![CDATA[" + auth_url + "]]></Url>";//添加主平台关注链接
                            strresponse1 = strresponse1 + "</item>";
                            strresponse1 = strresponse1 + "</Articles>";
                            strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                            strresponse1 = strresponse1 + "</xml>";
                            Response.Write(strresponse1);
                        }
                        #endregion
                        else
                        {
                            //用户输入的内容不是系统指令，进行正常查询
                            #region 词条搜索2
                            List<string> content1 = search(content, ToUserName);
                            int count = content1.Count + 1;
                            string image_url = "http:/" + "/lcsy2008.eicp.net/wbkpro/QRCODE/daohang.jpg";
                            //content1[0].Insert(content1[0].Length, count.ToString());
                            if (content1[0].Equals("没有搜索结果!") == false && content1[0].Equals("您输入的信息格式无法识别!") == false)
                            {
                                string strresponse = "<xml>";
                                strresponse = strresponse + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse = strresponse + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse = strresponse + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse = strresponse + "<MsgType><![CDATA[news]]></MsgType>";
                                strresponse = strresponse + "<ArticleCount>" + count.ToString() + "</ArticleCount>";
                                strresponse = strresponse + "<Articles>";

                                strresponse = strresponse + "<item>";
                                strresponse = strresponse + " <Title><![CDATA[您的搜索内容" + content + "得到了" + content1.Count.ToString() + "条搜索结果，我们抽取了前五名结果返回给您，您可以点击以下链接进入详细信息查看页面]]></Title>";
                                strresponse = strresponse + "<Description><![CDATA[]]></Description>";
                                strresponse = strresponse + "<PicUrl><![CDATA[" +image_url+ "]]></PicUrl>";
                                strresponse = strresponse + "<Url><![CDATA[" + "]]></Url>";
                                strresponse = strresponse + "</item>";
                                for (int i = 0; i < count - 1; i++)
                                {
                                    string pid = content1[i].Substring(9, 6);
                                    content1[i] = content1[i].Remove(0, 15);
                                    //string image_url = "http:/"+"/lcsy2008.eicp.net/wbkpro/product_image/" + pid + "_image.jpg";
                                    string url = "http://lcsy2008.eicp.net/wbkpro/mobile_product.aspx?pid=" + pid;
                                    strresponse = strresponse + "<item>";
                                    strresponse = strresponse + " <Title><![CDATA[" + content1[i] + "]]></Title>";
                                    strresponse = strresponse + "<Description><![CDATA[]]></Description>";
                                    strresponse = strresponse + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                                    strresponse = strresponse + "<Url><![CDATA[" + url + "]]></Url>";
                                    strresponse = strresponse + "</item>";
                                }
                                strresponse = strresponse + "</Articles>";
                                strresponse = strresponse + "<FuncFlag>1</FuncFlag>";
                                strresponse = strresponse + "</xml>";
                                //WriteLog(FromUserName + " " + content + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString());
                                //后期记录用户数据
                                Response.Write(strresponse);
                            }
                            else
                            {
                                string strresponse1 = "<xml>";
                                strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                                strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                                strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                                strresponse1 = strresponse1 + "<MsgType><![CDATA[text]]></MsgType>";
                                if (content1[0].Equals("您输入的信息格式无法识别!") == true)
                                    strresponse1 = strresponse1 + " <Content><![CDATA[" + content1[0] + content1[1] + "]]></Content>";
                                else
                                    strresponse1 = strresponse1 + " <Content><![CDATA[" + content1[0] + "]]></Content>";
                                strresponse1 = strresponse1 + "<FuncFlag>0</FuncFlag>";
                                strresponse1 = strresponse1 + "</xml>";
                                //WriteLog(FromUserName + " " + content + DateTime.Now.ToLongDateString() + DateTime.Now.ToLongTimeString());
                                Response.Write(strresponse1);
                            }
                            #endregion
                        }
                    }
                    else if (msgtype.Equals("location") == true)
                    {
                        string loc_x = xn.SelectSingleNode("//Location_X").InnerText;
                        string loc_y = xn.SelectSingleNode("//Location_Y").InnerText;
                        string loc_info = xn.SelectSingleNode("//Label").InnerText;
                        string strresponse2 = "<xml>";
                        strresponse2 = strresponse2 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                        strresponse2 = strresponse2 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                        strresponse2 = strresponse2 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                        strresponse2 = strresponse2 + "<MsgType><![CDATA[text]]></MsgType>";
                        strresponse2 = strresponse2 + " <Content><![CDATA[" + Url1 + "\n" + loc_info + "\n纬度：" + loc_x + "\n经度：" + loc_y + "\n" + "]]></Content>";
                        strresponse2 = strresponse2 + "<FuncFlag>0</FuncFlag>";
                        strresponse2 = strresponse2 + "</xml>";
                        Response.Write(strresponse2);
                    }
                }

                //用户仍未授权
                else
                {
                    string auth_url = "http://lcsy2008.eicp.net/wbkpro/mobile_authorize.aspx?openid=" + FromUserName + "&enterid=" + enter_id;//添加能从互联网直接访问的完整的mobile_authorize路径
                    string strresponse1 = "<xml>";
                    strresponse1 = strresponse1 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
                    strresponse1 = strresponse1 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
                    strresponse1 = strresponse1 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
                    strresponse1 = strresponse1 + "<MsgType><![CDATA[news]]></MsgType>";
                    strresponse1 = strresponse1 + "<ArticleCount>1</ArticleCount>";
                    strresponse1 = strresponse1 + "<Articles>";
                    strresponse1 = strresponse1 + "<item>";
                    strresponse1 = strresponse1 + " <Title><![CDATA[是否授权" + enter_name + "获取您的部分个人信息？]]></Title>";
                    strresponse1 = strresponse1 + "<Description><![CDATA[是否授权" + enter_name + "获取您的部分个人信息？]]></Description>";
                    strresponse1 = strresponse1 + "<PicUrl><![CDATA[" + "]]></PicUrl>";
                    strresponse1 = strresponse1 + "<Url><![CDATA[" + auth_url + "]]></Url>";
                    strresponse1 = strresponse1 + "</item>";
                    strresponse1 = strresponse1 + "</Articles>";
                    strresponse1 = strresponse1 + "<FuncFlag>1</FuncFlag>";
                    strresponse1 = strresponse1 + "</xml>";

                    Response.Write(strresponse1);
                }
            }

           #endregion
        }
        else
        {//啥也不是
            string strresponse4 = "<xml>";
            strresponse4 = strresponse4 + "<ToUserName><![CDATA[" + FromUserName + "]]></ToUserName>";
            strresponse4 = strresponse4 + "<FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";
            strresponse4 = strresponse4 + "<CreateTime>" + DateTime.Now.Ticks.ToString() + "</CreateTime>";
            strresponse4 = strresponse4 + "<MsgType><![CDATA[text]]></MsgType>";
            strresponse4 = strresponse4 + " <Content><![CDATA[" + ToUserName+ "]]></Content>";
            strresponse4 = strresponse4 + "<FuncFlag>0</FuncFlag>";
            strresponse4 = strresponse4 + "</xml>";
            Response.Write(strresponse4);
        }
           

    }

    /// <summary>
    /// 判断用户交互的子平台是否是注册用户，若不是不返回请求
    /// </summary>
    /// <param name="tousername"></param>
    /// <returns></returns>
    public bool exist(string tousername)
    {
        SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc1.Open();
        
        string sqlstr = "select ENTER_ID from INFO_ENTER where ENTER_WXACOUNT=@acc";
        SqlCommand com_exist = new SqlCommand(sqlstr,sc1);
        com_exist.Parameters.AddWithValue("@acc",tousername);
        if (com_exist.ExecuteScalar() != null)
        {
            sc1.Close();
            return true;
        }
        else { sc1.Close(); return false; }
    }

    

    private void WriteLog(string strMemo)
    {

        string filename = Server.MapPath("log.txt");
        StreamWriter sr = new StreamWriter(filename, true);
        sr.WriteLine(strMemo);
        sr.Close();

    }
}